/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_control_TInteger.h"

using namespace frm;
using namespace control;

// TInteger ///////////////////////////////////////////////////////////////////

//
void TInteger::SetValue(svl::TVariant V) 
{ 
  if (V.IsNull()) 
    { SetNull(); } 
  else 
    { SetValue(V.Integer()); } 
}

//
bool TInteger::UpdateValue(svl::TString V)
{
/***********
  int L = V.Len(); char *S = V; for (int N = 0; N < L; N++)
  {
    int Ch = S[N]; if (Ch == '-')
    { 
      if (N != 0) 
      { 
        if (S[0] == '-') { return false; }
        V.DelSym(N); V.AddToLeft(1,'-'); S = V;
      }
    }
    else if (Ch == '+')
    {
      int NMinus; if (!V.SearchSym('-',NMinus)) { return false;}
      //if (S[0] != '-') { return false;}
      V.DelSym(N); V.DelSym(0); N -= 2; N = max(0,N); L -= 2; S = V; 
      frm::TFormFocus *Focus = GetFocus(); if (Focus && Focus->Get() == this)
        { Focus->SetDeltaPos(-2); }
    }
    else if (!::isdigit(Ch))
      { return false; }
  }
  if (L > 1) { svl::TString Sv(V.ConvertToInt()); if (!Sv.CompareIC(V)) { return false; } }
***************/  
  return TString::UpdateValue(V);
}

//
void TInteger::TComponent_KeyboardChar(grf::TKeyboardChar &KeyboardChar)
{
  if( !enableEdit )
	  return;

  if (KeyboardChar.Key == '-')
  {
    svl::TString V = TString::GetValue(); if (V[0] != '-')
    { 
      BeginPaint();
      V.AddToLeft(1,'-'); UpdateValue(V); 
      GetFocus().SetDeltaPos(1);
      EndPaint();
    }
    return;
  }
  else if (KeyboardChar.Key == '+')
  {
    svl::TString V = TString::GetValue(); if (V[0] == '-')
    { 
      BeginPaint();
      V.DelSym(0); UpdateValue(V); 
      GetFocus().SetDeltaPos(-1);
      EndPaint();
    }
    return;
  }
  else if (!::isdigit(KeyboardChar.Key)) 
    { return;}
  else 
  {
    svl::TString V = TString::GetValue(); 
    V.InsSym(Focus_GetPos(),KeyboardChar.Key);
    
    if (V.Len() > 1)
    { 
      svl::TString Sv(V.ConvertToInt()); if (!Sv.CompareIC(V)) 
        { return; } 
    }
  }
  TString::TComponent_KeyboardChar(KeyboardChar);
}

//
void TInteger::TComponent_FocusExit()
{
  svl::TString V = TString::GetValue(); if (V.Len() == 1 && V[0] == '-')
    { UpdateValue("");}
  TString::TComponent_FocusExit();
}
